package es.udc.emarketapp.http.view.actionForms;

import java.util.Collection;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.emarket.model.countryFacade.CountryFacade;
import es.udc.emarketapp.http.util.struts.action.EMarketPropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.DefaultActionForm;
import java.util.Calendar;
import org.apache.struts.action.ActionMessage;

public class ClientForm extends DefaultActionForm {

    public final static String REGISTERCLIENT_ACTION = "REGISTERCLIENT";
    public final static String UPDATECLIENT_ACTION = "UPDATECLIENT";
    public final static String CHANGE_PASS_ACTION = "CHANGEPASS";
    private final static Collection<String> ACTION_TYPES = Arrays.asList(
            new String[]{CHANGE_PASS_ACTION, REGISTERCLIENT_ACTION,
                 UPDATECLIENT_ACTION});
    private String idClient;
    private String pass;
    private String retypePass;
    private String oldPassword;
    private String name;
    private String surname1;
    private String surname2;
    private String nifCif;
    private String address;
    private String city;
    private String cp;
    private String province;
    private String country;
    private String email;
    private String tlf1;
    private String tlf2;
    private String birthDay;
    private String birthMonth;
    private String birthYear;
    private Long birthDayAsLong;
    private Long birthMontAsLong;
    private Long birthYearAsLong;
    private Calendar birthDate;
    private String addressSend;
    private String citySend;
    private String cpSend;
    private String provinceSend;
    private String countrySend;
    private boolean sameAddress;
    private String action;

    public ClientForm() {
        reset();
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddressSend() {
        return addressSend;
    }

    public void setAddressSend(String addressSend) {
        this.addressSend = addressSend;
    }

    public Calendar getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Calendar birthDate) {
        this.birthDate = birthDate;
    }

    public String getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(String birthDay) {
        this.birthDay = birthDay;
    }

    public Long getBirthDayAsLong() {
        return birthDayAsLong;
    }

    public void setBirthDayAsLong(Long birthDayAsLong) {
        this.birthDayAsLong = birthDayAsLong;
    }

    public Long getBirthMontAsLong() {
        return birthMontAsLong;
    }

    public void setBirthMontAsLong(Long birthMontAsLong) {
        this.birthMontAsLong = birthMontAsLong;
    }

    public String getBirthMonth() {
        return birthMonth;
    }

    public void setBirthMonth(String birthMonth) {
        this.birthMonth = birthMonth;
    }

    public String getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(String birthYear) {
        this.birthYear = birthYear;
    }

    public Long getBirthYearAsLong() {
        return birthYearAsLong;
    }

    public void setBirthYearAsLong(Long birthYearAsLong) {
        this.birthYearAsLong = birthYearAsLong;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCitySend() {
        return citySend;
    }

    public void setCitySend(String citySend) {
        this.citySend = citySend;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCountrySend() {
        return countrySend;
    }

    public void setCountrySend(String countrySend) {
        this.countrySend = countrySend;
    }

    public String getCp() {
        return cp;
    }

    public void setCp(String cp) {
        this.cp = cp;
    }

    public String getCpSend() {
        return cpSend;
    }

    public void setCpSend(String cpSend) {
        this.cpSend = cpSend;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getIdClient() {
        return idClient;
    }

    public void setIdClient(String idClient) {
        this.idClient = idClient;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNifCif() {
        return nifCif;
    }

    public void setNifCif(String nifCif) {
        this.nifCif = nifCif;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getProvinceSend() {
        return provinceSend;
    }

    public void setProvinceSend(String provinceSend) {
        this.provinceSend = provinceSend;
    }

    public String getRetypePass() {
        return retypePass;
    }

    public void setRetypePass(String retypePass) {
        this.retypePass = retypePass;
    }

    public String getSurname1() {
        return surname1;
    }

    public void setSurname1(String surname1) {
        this.surname1 = surname1;
    }

    public String getSurname2() {
        return surname2;
    }

    public void setSurname2(String surname2) {
        this.surname2 = surname2;
    }

    public String getTlf1() {
        return tlf1;
    }

    public void setTlf1(String tlf1) {
        this.tlf1 = tlf1;
    }

    public String getTlf2() {
        return tlf2;
    }

    public void setTlf2(String tlf2) {
        this.tlf2 = tlf2;
    }

    public boolean getSameAddress() {
        return sameAddress;
    }

    public void setSameAddress(boolean sameAddress) {
        this.sameAddress = sameAddress;
    }

    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        PropertyValidator.validateString(errors, "action", action, true,
                ACTION_TYPES);


            if (REGISTERCLIENT_ACTION.equals(action)
                    || UPDATECLIENT_ACTION.equals(action)) {
                PropertyValidator.validateMandatory(errors, "name", name);
                PropertyValidator.validateMandatory(errors, "surname1",
                        surname1);
                EMarketPropertyValidator.validateString(errors, "nifCif",
                        nifCif, 9, true);
                PropertyValidator.validateMandatory(errors, "email", email);
                PropertyValidator.validateMandatory(errors, "address",
                        address);
                PropertyValidator.validateMandatory(errors, "city", city);
                PropertyValidator.validateMandatory(errors, "cp", cp);
                PropertyValidator.validateMandatory(errors, "province",
                        province);
                PropertyValidator.validateMandatory(errors, "country", country);
                PropertyValidator.validateMandatory(errors, "tlf1", tlf1);
     
                if (!(sameAddress)) {
                    PropertyValidator.validateMandatory(errors,
                            "addressSend", addressSend);
                    PropertyValidator.validateMandatory(errors,
                            "citySend", citySend);
                    PropertyValidator.validateMandatory(errors,
                            "cpSend", cpSend);
                    PropertyValidator.validateMandatory(errors,
                            "provinceSend", provinceSend);
                    PropertyValidator.validateMandatory(errors,
                            "countrySend", countrySend);
                }else{
                    addressSend = address;
                    citySend = city;
                    cpSend = cp;
                    provinceSend = province;
                    countrySend = country;
                }


                birthDayAsLong = PropertyValidator.validateLong(errors, 
                        "birthDay", birthDay, true, Long.MIN_VALUE,
                        Long.MAX_VALUE);
                birthMontAsLong = PropertyValidator.validateLong(errors, 
                        "birthMonth", birthMonth, true, Long.MIN_VALUE,
                        Long.MAX_VALUE);
                birthYearAsLong = PropertyValidator.validateLong(errors, 
                        "birthYear", birthYear, true, Long.MIN_VALUE,
                        Long.MAX_VALUE);
                boolean isValid = true;
                isValid = EMarketPropertyValidator.validateBirthDate(errors,
                        "birthYear", birthYearAsLong, "birthMonth",
                         birthMontAsLong, "birthDay", birthDayAsLong);
                if (isValid) {
                    birthDate.set(birthYearAsLong.intValue(),
                            birthMontAsLong.intValue() - 1,
                            birthDayAsLong.intValue());
                }

                if (email != null && email.compareTo("") != 0) {
                    EMarketPropertyValidator.validateEmailAddress(errors,
                            "email", email);
                }

                
                if (REGISTERCLIENT_ACTION.equals(action)) {
                    EMarketPropertyValidator.validateString(errors, "idClient",
                            idClient, 8, true);
                    EMarketPropertyValidator.validateString(errors, "pass", pass,
                            8, true);
                    EMarketPropertyValidator.validateString(errors, "retypePass",
                            retypePass, 8, true);

                    if (pass != null) {
                        if (retypePass != null) {
                            if (!pass.equals(retypePass)) {
                                errors.add("retypePass",
                                        new ActionMessage("ErrorMessages.password.doNotMatch"));
                            }
                        } else {
                            errors.add("retypePass",
                                    new ActionMessage("ErrorMessages.password.doNotMatch"));
                        }
                    } else {
                        if (retypePass != null) {
                            errors.add("retypePass",
                                    new ActionMessage("ErrorMessages.password.doNotMatch"));
                        }
                    }
                }

            } else if (CHANGE_PASS_ACTION.equals(action)) {

                System.out.println(oldPassword);
                if (oldPassword != null) {
                    if (oldPassword == null || oldPassword.compareToIgnoreCase("") == 0) {
                        errors.add("oldPassword",
                                new ActionMessage("ErrorMessages.mandatoryField"));
                    } else {
                        System.out.println(this.oldPassword);
                    }
                    if (retypePass != null) {
                        if (!retypePass.equals(retypePass)) {
                            errors.add("retypePass",
                                    new ActionMessage("ErrorMessages.password.doNotMatch"));
                        }
                    } else {
                        errors.add("retypePass",
                                new ActionMessage("ErrorMessages.password.doNotMatch"));
                    }
                } else {
                    if (retypePass != null) {
                        errors.add("retypePass",
                                new ActionMessage("ErrorMessages.password.doNotMatch"));
                        if (oldPassword == null || oldPassword.compareToIgnoreCase("") == 0) {
                            errors.add("oldPassword",
                                    new ActionMessage("ErrorMessages.mandatoryField"));
                        } else {
                            System.out.println(this.oldPassword);
                        }

                    }
                }
            }

        /*

        if (!errors.isEmpty()) {
           // request.setAttribute("dateRanges", new DateRanges());

            try {
                CountryFacade countryFacade = new CountryFacade();
                List<CountryVO> countryVOs = countryFacade.findAllCountries();
                request.setAttribute("countries", countryVOs);
            } catch (Exception e) {
            }

        }
         * 
         */

        return errors;

    }

    private void reset() {
        idClient = null;
        pass = null;
        retypePass = null;
        name = null;
        surname1 = null;
        surname2 = null;
        nifCif = null;
        address = null;
        city = null;
        cp = null;
        province = null;
        country = null;
        email = null;
        tlf1 = null;
        tlf2 = null;
        birthDay = null;
        birthMonth = null;
        birthYear = null;
        birthDayAsLong = new Long(-1);
        birthMontAsLong = new Long(-1);
        birthYearAsLong = new Long(-1);
        addressSend = null;
        citySend = null;
        cpSend = null;
        provinceSend = null;
        countrySend = null;
        sameAddress = false;

        birthDayAsLong = new Long(-1);
        birthMontAsLong = new Long(-1);
        birthYearAsLong = new Long(-1);

        birthDate = Calendar.getInstance();
        birthDate.clear();

    }
}
